package cn.hx.admin.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.hx.admin.entity.User;
import cn.hx.admin.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息表(User)表控制层
 *
 * @author makejava
 * @since 2023-12-18 16:26:07
 */
@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<List<User>> queryByPage(User user) {
        return ResponseEntity.ok(this.userService.queryByPage(user));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<User> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.userService.queryById(id));
    }

    @GetMapping("excel")
    public void excel(HttpServletRequest request, HttpServletResponse response) {
        List<User> list = userService.queryByPage(null);
        ExportParams params = new ExportParams("计算机一班学生", "学生信息", ExcelType.XSSF);
        params.setFreezeCol(2);

        Map map = new HashMap<>();
        map.put(NormalExcelConstants.DATA_LIST, list);
        map.put(NormalExcelConstants.CLASS, User.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "学生信息");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    @PostMapping("excelIn")
    public void excel(MultipartFile file) throws IOException {
        File file1 = new File("D:/学生统计1.xls");
        file.transferTo(file1);
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        List<User> list = ExcelImportUtil.importExcel(file1, User.class, params);
        System.out.println(list);
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<User> add(User user) {
        User insert = this.userService.insert(user);
        List<Integer> postIds = user.getPostIds();
        if (postIds != null && !postIds.isEmpty()) {
            userService.insertUserPost(user.getUserId(), postIds);

        }
        return ResponseEntity.ok(insert);
    }

    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<User> edit(@RequestBody User user) {
        List<Integer> postIds = user.getPostIds();
        if (postIds != null && !postIds.isEmpty()) {
            userService.deleteUserPostByUserId(user.getUserId());

            userService.insertUserPost(user.getUserId(), postIds);

        }

        System.out.println(user);
        return ResponseEntity.ok(this.userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.userService.deleteById(id));
    }

}

